//
//  THJQuickLoginView.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/1/2.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJQuickLoginView.h"
#import "NSString+String.h"
@implementation THJQuickLoginView
{
    NSString * token;
}
+(THJQuickLoginView*)instancView{
    
    NSArray * nibView =  [[NSBundle mainBundle] loadNibNamed:@"THJQuickLoginView" owner:nil options:nil];
    return [nibView objectAtIndex:0];
}
- (IBAction)getCode:(id)sender {
    BOOL isPhoneNum = [NSString isMobileNumber:self.phoneText.text];
    if (!isPhoneNum) {
        [self HUDShowMsg:@"请输入正确手机号" AfterDelayDimiss:1];
        return;
    }
    NSString * urlStr = [NSString stringWithFormat:@"%@index.php?f=index&r=index&m=code&mobile=%@",THJUrlDomain,self.phoneText.text];
    __weak typeof(self) weakself = self;
    [THJNetWork getNetDataFromUrl:urlStr callback:^(NSDictionary* receiveData, NSError *error) {
        [weakself HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if (!error) {
            NSLog(@"yanzhegnma-%@",receiveData);
            if ([receiveData[@"status"] isEqual:@"200"]) {
                token = receiveData[@"data"];
               [weakself startWithSecond:59];
            }
        }
    }];
}
- (IBAction)logInAction:(id)sender {
    BOOL isOrNo = [NSString isMobileNumber:self.phoneText.text];
    if (!isOrNo) {
        [self HUDShowMsg:@"请输入正确手机号码" AfterDelayDimiss:1];
        return;
    }
    if(self.yanzhengmaTf.text.length==0){
        [self HUDShowMsg:@"请输入验证码" AfterDelayDimiss:1];
        return;
    }
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=login&r=login&m=mlogin"];
    NSDictionary * paraDic = @{
                               @"code":self.yanzhengmaTf.text,
                               @"mobile":self.phoneText.text,
                               };
    __weak typeof(self) weakself = self;
    [THJNetWork postDataFromUrl:urlstr parameters:paraDic Token:token callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"验证码快速登录-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            [[NSUserDefaults standardUserDefaults] setObject:receiveData[@"data"] forKey:@"token"];
            [[NSUserDefaults standardUserDefaults] setObject:self.phoneText.text forKey:@"MOBILE"];
            [weakself performSelector:@selector(pop1) withObject:nil afterDelay:1];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"login" object:nil];
        }
    }];
}
-(void)pop1{
    [self.delegate loginSuccesed];
}
-(void)HUDShowMsg:(NSString*)msg AfterDelayDimiss:(double)second{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.mode = MBProgressHUDModeText;
    hud.label.text = msg;
    //hud.label.backgroundColor = COLOR(0, 0, 0, 0.7);
    hud.removeFromSuperViewOnHide = YES;
    [hud hideAnimated:YES afterDelay:second];
}
//按钮开始倒计时
-(void)startWithSecond:(NSInteger)second{
    __weak typeof(self) weakself = self;
    __block NSInteger timeout=second; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [weakself.yzmBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                weakself.yzmBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                NSLog(@"____%@",strTime);
                weakself.yzmBtn.titleLabel.text = strTime;
                [weakself.yzmBtn setTitle:[NSString stringWithFormat:@"%@",strTime] forState:UIControlStateNormal];
                weakself.yzmBtn.userInteractionEnabled = NO;
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}
@end
